home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 126-150 / scopedisk132 / viewdir / viewdir.asm < prev    next >
Assembly Source File  |  1995-03-19  |  16KB  |  536 lines

  1. ;   ViewDir 2.0/ size directory - Jim Butterfield.  April 5/90.
  2. ; Exec library calls
  3. _LVOAllocMem          EQU -$C6
  4. _LVOFreeMem           EQU -$D2
  5. _LVOSetSignal         EQU -$132
  6. _LVOCloseLibrary      EQU -$19E
  7. _LVOOpenLibrary       EQU -$228
  8. ; DOS library calls
  9. _LVOOpen              EQU -$1E
  10. _LVOClose             EQU -$24
  11. _LVORead              EQU -$2A
  12. _LVOWrite             EQU -$30
  13. _LVOOutput            EQU -$3C
  14. _LVOLock              EQU -$54
  15. _LVOUnLock            EQU -$5A
  16. _LVOExamine           EQU -$66
  17. _LVOExNext            EQU -$6C
  18. _LVOCurrentDir        EQU -$7E
  19. _LVOIoErr             EQU -$84
  20. ;
  21. ACCESS_READ            equ -2
  22. MODE_OLDFILE           equ 1005
  23. INFOSIZE               equ 260
  24. DATASIZE               equ 100
  25. ; FileInfoBlock items
  26. fib_FileName           equ 8
  27. fib_EntryType          equ 120
  28. fib_Size               equ 124
  29. fib_NumBlocks          equ 128
  30. ; IO Error Type
  31. NO_MORE_ENTRIES        equ 232
  32. ;
  33. ;-- Initial setup:
  34. ;        offsets from A5
  35. tSize    equ  -4
  36. tCount   equ  -8
  37. dosBase  equ  -12
  38. argv     equ  -16
  39. OutHandle equ -20
  40. DataBlk  equ  -24
  41. Blocker  equ  -25    ;byte
  42. KillInf  equ  -26    ;byte
  43. Look     equ  -27    ;byte
  44. FileErr  equ  -28    ;byte
  45. Level    equ  -30    ;word
  46. String   equ  -42    ;12 bytes
  47.  
  48.        link   a5,#-44          ; common work area
  49.        moveq  #0,d1
  50.        move.l d1,FileErr(a5)  ; four bytes
  51. trsp   move.b d1,-1(a0,d0)    ; c style string
  52.        move.b -2(a0,d0),d1
  53.        cmp.b  #$20,d1
  54.        bne.s  lastCh
  55.        subq.w #1,d0
  56.        bra.s  trsp
  57. lastCh sub.b  #$22,d1         ; kill trailing quote
  58.        bne.s  spdump
  59.        move.b d1,-2(a0,d0)
  60. spdump move.b (a0)+,d0
  61.        cmp.b  #$20,d0
  62.        beq.s  spdump
  63.        cmp.b  #$22,d0         ; leading quote
  64.        beq.s  skipq
  65.        cmp.b  #$2d,d0         ;dash
  66.        beq.s  fgdump
  67.        sub.b  #'?',d0
  68.        beq.s  hook
  69.        bra.s  spbak
  70. fgdump move.b (a0)+,d0
  71.        beq.s  hook
  72.        and.b  #$5f,d0
  73.        cmp.b  #$49,d0         ;-i
  74.        bne.s  NotI
  75.        move.b d0,KillInf(a5)
  76.        bra.s  fgdump
  77. NotI   cmp.b  #$54,d0
  78.        bne.s  NotT
  79.        move.b d0,Look(a5)
  80.        bra.s  fgdump
  81. NotT   cmp.b  #$42,d0        ;-b
  82.        bne.s  spdump
  83.        move.b d0,Blocker(a5)
  84.        bra.s  fgdump
  85. skipq  addq.l #1,a0
  86. spbak  move.b -(a0),d0
  87.        move.l a0,argv(a5)
  88. ;**  Level=0;  tCount=0;
  89. hook   moveq  #0,d1
  90.        move.l d1,Level(a5)
  91.        move.l d1,tCount(a5)
  92. ; set up dos, output, here
  93.        movem.l a0/a1/d0/d1,-(A7)
  94.        move.b #1,FileErr(a5)
  95.        lea    dosname(pc),a1    ; Name 'dos.library'.
  96.        clr.l  d0                ;   Any version (0)
  97.        move.l $4,a6             ;   Using Exec library
  98.        jsr    _LVOOpenLibrary(a6)  ;   Open Dos library.
  99.        move.l d0,dosBase(a5)    ; Remember DosBase ptr.
  100.        beq    StartupQuit       ;      dos not opened
  101.        move.l d0,a6             ; set Dos library
  102.        jsr    _LVOOutput(a6)    ;   get CLI outhandle,
  103.        move.l d0,OutHandle(a5)       ;   & then remember it.
  104.        movem.l (a7)+,a0/a1/d0/d1
  105.        tst.l  d0                ; no file name?
  106.        beq.s  Prompt
  107. ;**    GetDir(argv[1]);
  108.        move.l $4,a6             ; this calls for Exec
  109.        moveq  #0,d1             ; any type of memory
  110.        moveq  #DATASIZE,d0      ; size of block needed
  111.        jsr    _LVOAllocMem(a6)  ; go get it
  112.        move.l dosBase(a5),a6    ; restore Dos Library
  113.        move.l d0,DataBlk(a5)    ; save the block address
  114.        beq.s  DosQuit           ; quit if no memory
  115.        move.l argv(a5),a0       ; file name
  116.        bsr.s  GetDir
  117.        move.l $4,a6             ; set Exec library
  118.        move.l DataBlk(a5),a1    ; memory block address
  119.        moveq  #DATASIZE,d0      ; size of block
  120.        jsr    _LVOFreeMem(a6)   ; release this memory
  121. DosQuit  move.l dosBase(a5),a6  ; set Dos Library
  122. ;**    switch (FileErr)
  123.        subq.b #1,FileErr(a5)
  124.        bne.s  Ncase1
  125. ;**      case 1:
  126. ;**        printf("Out of Memory\n");
  127.        move.l OutHandle(a5),d1       ;file handle
  128.        lea    NoMemMsg(pc),a1
  129.        move.l a1,d2             ;outbuffer
  130.        moveq  #NoMemLen,d3      ;length
  131.        jsr    _LVOWrite(A6)
  132.  
  133. ;**      case 2:
  134. ;**        printf("Can't find file.\n");
  135. Ncase1 subq.b #1,FileErr(a5)
  136.        bne.s  Ncase2
  137.        move.l OutHandle(a5),d1       ;file handle
  138.        lea    NoFileMsg(pc),a1
  139.        move.l a1,d2             ;outbuffer
  140.        moveq  #NoFileLen,d3     ;length
  141.        jsr    _LVOWrite(A6)
  142.  
  143. ;**      case 3:
  144. ;**        printf("^C\n");
  145. Ncase2 subq.b #1,FileErr(a5)
  146.        bne.s  endSwitch
  147.        move.l OutHandle(a5),d1       ;file handle
  148.        lea    CtrlCMsg(pc),a1
  149.        move.l a1,d2
  150.        moveq  #CtrlCLen,d3
  151.        jsr    _LVOWrite(A6)
  152. endSwitch   bra.s  LastGasp
  153. ;**      }
  154. ;**    }
  155. ;**  else printf("FORMAT:  ViewDir <DirectoryName>\n");
  156. Prompt      move.l OutHandle(a5),d1
  157.        lea    PromptMsg(pc),a1
  158.        move.l a1,d2
  159.        moveq  #PromptLen,d3
  160.        jsr    _LVOWrite(A6)
  161. ;**  }
  162. LastGasp:   move.l dosBase(a5),a1    ; DosBase to a1;
  163.        move.l $4,a6             ;   then using Exec library,
  164.        jsr    _LVOCloseLibrary(a6)    ;   close Dos library.
  165. StartupQuit unlk   a5
  166.        rts                      ; End of Program
  167.  
  168. ;**void GetDir(char * Name)
  169. ;**  {
  170. ;**  unsigned long Size, Count, newLock, oldLock;
  171. ;**  struct FileInfoBlock *fibb;
  172. ;**  char *NameSave;
  173. ;            offsets from A4
  174. Size        equ    -4
  175. Count       equ    -8
  176. newLock     equ    -12
  177. oldLock     equ    -16
  178. fibb        equ    -20
  179. NameSave    equ    -24    ;drawer (calling) name
  180. xName       equ    -28    ;name to be printed
  181. fType       equ    -32    ;dir or file
  182.  
  183. GetDir link   A4,#-32
  184.        move.l a0,xName(a4)
  185.        move.l a0,NameSave(a4)
  186. ;**  Size = 0;
  187. ;**  Count =0;
  188. ;**  Count = 0;
  189.        moveq  #0,d0
  190.        move.l d0,Size(a4)
  191.        move.l d0,Count(a4)
  192. ;**  FileErr =1;   /* signal Alloc error */
  193.        move.b #1,FileErr(a5)
  194. ;**  if ((fibb = AllocMem(INFOSIZE,0)) != 0)
  195.        move.l $4,a6             ; this calls for Exec
  196.        moveq  #0,d1             ; any type of memory
  197.        move.l #INFOSIZE,d0      ; size of block needed
  198.        jsr    _LVOAllocMem(a6)  ; go get it
  199.        move.l dosBase(a5),a6    ; restore Dos Library
  200.        move.l d0,fibb(a4)       ; save the block address
  201.        beq    GDExit            ; quit if no memory
  202. ;**    {
  203. ;**      FileErr =2;   /* signal file unreadable */
  204.        move.b #2,FileErr(a5)
  205. ;**      if ((newLock = Lock(Name,ACCESS_READ)) != 0)
  206.        move.l xName(a4),d1
  207.        moveq  #ACCESS_READ,D2   ; get a read lock
  208.        jsr    _LVOLock(a6)
  209.        move.l d0,newLock(a4)    ; store the lock pointer
  210.        beq    DropMem           ; couldn't get lock
  211. ;**      { if ((Examine(newLock,fibb)) != 0)
  212.        move.l d0,d1             ; lock
  213.        move.l fibb(a4),d2       ; file info block address
  214.        jsr _LVOExamine(a6)      ; go for the data
  215. ;   Assume Examine never fails.  Go for the data.
  216. ;**        {
  217. ;**        FileErr =0;    /* got file, normal error sequence */
  218.        move.b #0,FileErr(a5)
  219. ;**         {
  220. ;**              if (fibb->fib_EntryType < 0)
  221.        move.l fibb(a4),a3
  222.        move.l fib_EntryType(a3),d0
  223.        move.l d0,fType(a4)
  224.        bpl.s  TypeDir2
  225. ;**                {
  226. ;**                Size = fibb->fib_Size;
  227. ;**                Blox = fibb->fib_NumBlocks;
  228. ;**                Count = 1;
  229. ;**                }  /* file */
  230.        move.l NameSave(a4),a0
  231.        jsr    FilBit
  232.        bra    NextItem
  233. ;**              else
  234. TypeDir2:
  235. ;**           {
  236. ;**                oldLock = CurrentDir(newLock);
  237.        move.l newLock(a4),d1
  238.        jsr    _LVOCurrentDir(a6)
  239.        move.l d0,oldLock(a4)
  240. ;**                Level += 1;
  241.        addq.w #1,Level(a5)
  242. DirLoop:
  243. ;**    {
  244. ;**            if ((ExNext(newLock,fibb)) == 0)
  245.        move.l newLock(a4),d1   ; lock
  246.        move.l fibb(a4),d2      ; file info block address
  247.        jsr    _LVOExNext(a6)      ; go for the data
  248.        tst.l  d0
  249.        beq.s  EndDir
  250.        tst.b  FileErr(a5)
  251.        bne.s  EndDir
  252. ;**              if (fibb->fib_EntryType < 0)
  253.        move.l fib_EntryType(a3),d0
  254.        bpl.s  TypeDir3
  255.        lea    fib_FileName(a3),a0
  256.        jsr    FilBit
  257.        bra.s  Join3
  258. ;**                GetDir(fibb->fib_FileName);
  259. ;**                Size += tSize;
  260. ;**                Count += tCount;
  261. TypeDir3  lea    fib_FileName(a3),a0
  262.        bsr    GetDir
  263. Join3  move.l tSize(a5),d0
  264.        add.l  d0,Size(a4)
  265.        move.l tCount(a5),d0
  266.        add.l  d0,Count(a4)
  267. ; collect stats?
  268.        move.l fibb(a4),a3
  269.        bra.s  DirLoop
  270. ;**            Level -= 1;
  271. EndDir subq.w #1,Level(a5)
  272. ;**                newLock = CurrentDir(oldLock);
  273.        move.l oldLock(a4),d1
  274.        jsr    _LVOCurrentDir(a6)
  275.        move.l d0,newLock(a4)
  276. ;**                }  /* dir */
  277. Rejoin2:
  278. ;**              ??Count += Count;
  279.        tst.b  FileErr(a5)
  280.        bne.s  EndSplit1
  281.        move.l Size(a4),d7
  282. ;**              if (Level <1 0)
  283.        move.w Level(a5),d0
  284.        beq.s  Dir3     ; assuming a directory
  285.        subq.w #1,d0
  286.        bne.s  NextItem
  287. ;**                {
  288. ;**                else
  289. ;**                      printf(" DIR %3d",Count);
  290. Dir3   move.l OutHandle(a5),d1
  291.        lea    DirMsg(pc),a1
  292.        move.l a1,d2
  293.        moveq  #DirLen,d3
  294.        jsr    _LVOWrite(A6)
  295.        move.l Count(a4),d0
  296.        bsr    MakeAscii
  297.        move.l OutHandle(a5),d1
  298.        lea    String+5(A5),a1
  299.        move.l a1,d2
  300.        moveq  #5,d3
  301.        jsr    _LVOWrite(A6)
  302. ;**                printf(" %7d %4d %s\n",Size,Blox,NameSave);
  303.        move.l NameSave(a4),a2
  304.        bsr    PrintStuff
  305. ;**                }
  306. ; look for file join here
  307. NextItem:
  308.        move.l a6,d7
  309.        moveq  #0,d0
  310.        move.l #$1000,d1
  311.        move.l $4,a6
  312.        jsr    _LVOSetSignal(a6)
  313.        move.l d7,a6
  314.        and.l  #$1000,d0
  315.        beq.s  EndSplit1
  316.        move.b #3,FileErr(a5)
  317. EndSplit1:
  318. ;**   tSize = Size;
  319. ;**   tCount = Count;
  320.        move.l Size(a4),tSize(a5)
  321.        move.l Count(a4),tCount(a5)
  322. ;**        }   /* Examine OK */
  323. ;**      UnLock(newLock);
  324. DropLock    move.l dosBase(a5),a6    ; set Dos Library
  325.        move.l newLock(a4),d1    ; take off the lock
  326.        jsr    _LVOUnLock(a6)
  327. ;**      }      /* Lock OK */
  328. ;**    FreeMem(fibb,INFOSIZE);
  329. DropMem:    move.l $4,a6             ; set Exec library
  330.        move.l fibb(a4),a1       ; memory block address
  331.        move.l #INFOSIZE,d0      ; size of block
  332.        jsr    _LVOFreeMem(a6)   ; release this memory
  333.        move.l dosBase(a5),a6    ; set Dos Library
  334. ;**    }        /* Alloc OK */
  335. GDExit unlk   A4
  336. FilEnd rts
  337. ;**  }
  338.  
  339. FilBit move.l  a0,xName(a4)
  340.        move.l  fib_Size(a3),d7
  341.        tst.b   Blocker(a5)
  342.        beq.s   noblox2
  343.        move.l  fib_NumBlocks(a3),d7
  344. noblox2 move.l d7,tSize(a5)
  345.        moveq   #1,d0
  346.        move.l  d0,tCount(a5)
  347. ;**              if (Level <1 0)
  348.        move.w Level(a5),d0
  349.        beq.s  pf          ; assuming a file
  350.        subq.w #1,d0
  351.        bne.s  FilEnd
  352. ; Check .info status
  353. pf     tst.b  KillInf(a5)
  354.        beq.s  PeekFile
  355.        move.l xName(a4),a2
  356. TryCharx  move.b (a2)+,d0
  357.        bne.s  TryCharx
  358.        lea    -6(a2),a2
  359.        lea    InfoLine(pc),a1
  360. InfLp  cmp.b  (a1)+,(a2)+
  361.        beq.s  InfLp
  362.        tst.b  -1(a2)
  363.        beq.s  FilEnd
  364. ; We're gonna print the file.  Empty?  Will we look at it?
  365. PeekFile lea   EmptyMsg(pc),a2
  366.        tst.l  d7
  367.        beq    FlEmp
  368.        tst.b  Look(a5)
  369.        bne    DoNotLook
  370. ; Open the file in order to check details
  371.        move.l xName(a4),d1   ; filename pointer
  372.        move.l #MODE_OLDFILE,d2  ; get a read lock
  373.        jsr    _LVOOpen(a6)
  374.        move.l d0,d6             ; store the file handle
  375.        beq    FlEmp             ; couldn't get file
  376. ; Read DATASIZE bytes of the file, then close it
  377.        move.l d6,d1             ;infile handle
  378.        move.l DataBlk(a5),d2    ; input buffer address
  379.        moveq  #DATASIZE,d3      ; how many bytes
  380.        jsr    _LVORead(a6)      ; so read it
  381.        move.l d0,d5             ; actual bytes read
  382.        move.l d6,d1             ; inhandle
  383.        jsr    _LVOClose(a6)       ; close the file
  384.        lea    TextMsg(pc),a2
  385. ; check for text characters only
  386.        move.l DataBlk(a5),a0
  387.        moveq  #0,d3
  388. TxLp   move.b 0(a0,d3),d0
  389.        cmp.b  #9,d0
  390.        beq.s  TryMore
  391.        cmp.b  #10,d0
  392.        beq.s  TryMore
  393.        cmp.b  #$20,d0
  394.        blt.s  NoText
  395. TryMore addq.w #1,d3
  396.        cmp.w  d3,d5
  397.        bne.s  TxLp
  398.        bra    FlEmp 
  399. ; Not Text - try longword start
  400. NoText move.l (a0),d0
  401.        lea    LdblMsg(pc),a2
  402.        cmp.l  #$3F3,d0
  403.        beq.s  FlEmp
  404. ; special IFF sequence
  405.        cmp.l  #'FORM',d0
  406.        bne.s  WordStart       
  407.        lea    String(a5),a2
  408.        move.l #'IFF:',(a2)
  409.        move.l 8(a0),4(a2)
  410.        bra.s  FlEmp
  411. ; look for magic word start
  412. WordStart  move.w (a0),d0
  413.        lea    WBOMsg(pc),a2
  414.        cmp.w  #$E310,d0
  415.        beq.s  FlEmp
  416.        lea    FLisMsg(pc),a2
  417.        cmp.w  #$f00,d0
  418.        beq.s  FlEmp
  419.        lea    WBWMsg(pc),a2
  420.        cmp.w  #$f34c,d0
  421.        beq.s  FlEmp
  422. ; special Basic sequence
  423.        and.w  #$FE7F,d0
  424.        lea    BasMsg(pc),a2
  425.        cmp.w  #$F400,d0
  426.        beq.s  FlEmp
  427. ; special longword-text test
  428.        moveq  #3,d1
  429. lngtst move.b 0(a0,d1.w),d0
  430.        cmp.b  #$30,d0
  431.        blt.s  DoNotLook
  432.        cmp.b  #$7f,d0
  433.        bgt.s  DoNotLook
  434.        dbf    d1,lngtst
  435.        lea    String(a5),a2
  436.        move.l #'....',(a2)
  437.        move.l (a0),4(a2)
  438.        bra.s  FlEmp
  439.  
  440.  
  441. DoNotLook lea FileMsg(pc),a2
  442. FlEmp  move.l a2,d2
  443.        move.l OutHandle(a5),d1
  444.        moveq  #FileLen,d3
  445.        jsr    _LVOWrite(a6)
  446.        move.l xName(a4),a2
  447. ; PeekFile drops into PrintStuff
  448. ;     ...or PS may be called directly...
  449. PrintStuff:
  450.        move.l d7,d0
  451.        bsr.s  MakeAscii
  452.        move.l OutHandle(a5),d1
  453.        lea    String(a5),a1
  454.        moveq  #11,d3
  455.        tst.b  Blocker(a5)
  456.        beq.s  noblox3
  457.        lea    String+4(a5),a1
  458.        moveq  #7,d3
  459. noblox3   move.l a1,d2
  460.        jsr    _LVOWrite(a6)
  461.        move.w Level(a5),d0
  462.        bne.s  Flush
  463.        move.l OutHandle(a5),d1
  464.        lea    SpaceMsg(pc),a1
  465.        move.l a1,d2
  466.        moveq  #SpaceLen,d3
  467.        jsr    _LVOWrite(A6)
  468. Flush  move.l OutHandle(a5),d1
  469.        move.l a2,d2
  470.        moveq  #0,d3
  471. TryChara  move.b 0(a2,d3),d0
  472.        beq.s  FoundEnd
  473.        addq.b #1,d3
  474.        bra.s  TryChara
  475. FoundEnd  jsr    _LVOWrite(A6)
  476.        move.l OutHandle(a5),d1
  477.        lea    MsgNewLine(pc),a1
  478.        move.l a1,d2
  479.        moveq  #NewLineLen,d3
  480.        jsr    _LVOWrite(A6)
  481. PrEnd  rts
  482.  
  483. MakeAscii:
  484.        moveq  #$20,d1
  485.        lea    String(a5),a1
  486.        moveq  #11,d2
  487. SpacedOut   move.b d1,0(a1,d2)
  488.        dbf    d2,SpacedOut
  489.        moveq  #9,d2
  490. DigLoop     swap   d0
  491.        moveq  #0,d1
  492.        move.w d0,d1
  493.        beq.s  Skipper
  494.        divu   #10,d1
  495. Skipper     move.w d1,d0
  496.        swap   d0
  497.        move.w d0,d1
  498.        divu   #10,d1
  499.        move.w d1,d0
  500.        swap   d1
  501.        or.b   #$30,d1
  502.        move.b d1,0(a1,d2)
  503.        tst.l  d0
  504.        dbeq   d2,DigLoop
  505.        rts
  506.  
  507. InfoLine    dc.b   '.info.'
  508. MsgNewLine  dc.b   $0a
  509. NewLineLen  equ    *-MsgNewLine
  510. SpaceMsg    dc.b   $20
  511. SpaceLen    equ    *-SpaceMsg
  512. DirMsg      dc.b   'DIR'
  513. DirLen      equ    *-DirMsg
  514. PromptMsg   dc.b   'ViewDir (by Jim Butterfield) V2.0',$0a
  515.             dc.b   'FORMAT:  ViewDir [-b -i -t] directory',$0a
  516. PromptLen   equ    *-PromptMsg
  517. TextMsg     dc.b   'TextFile'
  518. LdblMsg     dc.b   'Loadable'
  519. WBOMsg      dc.b   'WBObject'
  520. WBWMsg      dc.b   'WBWindow'
  521. BasMsg      dc.b   'AmiBasic'
  522. FLisMsg     dc.b   'FontList'
  523. FileMsg     dc.b   'FILE    '
  524. FileLen     equ    *-FileMsg
  525. EmptyMsg    dc.b   'Empty   ',$0a
  526. EmptyLen    equ    *-EmptyMsg
  527. NoMemMsg    dc.b   'Out of Memory',$0a
  528. NoMemLen    equ    *-NoMemMsg
  529. NoFileMsg   dc.b   'Can t find file.',$0a
  530. NoFileLen   equ    *-NoFileMsg
  531. CtrlCMsg    dc.b   '^C',$0a
  532. CtrlCLen     equ    *-CtrlCMsg
  533. dosname     dc.b   'dos.library',0
  534.             end
  535.  
  536.